﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.BLL.Core.IService;
using Empires.CRL.DataContract;
using Empires.RAL.Access;
using Empires.RAL.Pump;
using Empires.BLL.ServerProvider;

namespace Empires.BLL.Core.Implement
{
    public class CompanyDownloadService : ICompanyDownloadService
    {
        public string GetDownloadFileName(int id,string copname)
        {
            ICompanyDownloadAccess service = new CompanyDownloadDataPump();
            string fileName = service.GetDownloadFileName(id, copname);
            //ICompanyBaseService baseService = new CompanyBaseService();
            //string resourcePath = baseService.GetCompanyRescourceBasePath(copname, CompanyRescourceType.Files);
            //if (!resourcePath.StartsWith("/"))
            //{
            //    resourcePath = "/" + resourcePath;
            //}
            //if (!resourcePath.EndsWith("/"))
            //{
            //    resourcePath = resourcePath + "/";
            //}
            return fileName;
        }

        public List<DefaultDataPack> GetDownloadFiles(string copname, int type,int page,int take)
        {
            ICompanyDownloadAccess service = new CompanyDownloadDataPump();
            int itemCount = service.GetDownloadFilesItemCount(copname, type);
            return CompanyHelper.InjuectPagingInfo((currentPage) =>
            {
                return service.GetDownloadFiles(copname, type, currentPage, take);
            },
            itemCount, page, take);

            //int pagecount = itemCount % take == 0 ? itemCount / take : itemCount / take + 1;
            //page = page < 0 ? 0 : page;
            //page = page > pagecount - 1 ? pagecount - 1 : page;
            //int previouspage = page == 0 ? 0 : page - 1;
            //int nextpage = page == pagecount - 1 ? pagecount - 1 : page + 1;
            //var list = service.GetDownloadFiles(copname, type,page,take);

            //return list.Select(c =>
            //{
            //    c.ITEMCOUNT = itemCount;
            //    c.PAGESIZE = take;
            //    c.CURRENTPAGE = page + 1;
            //    c.PAGECOUNT = pagecount;
            //    c.PREVIOUSPAGE = previouspage;
            //    c.NEXTPAGE = nextpage;
            //    c.FIRSTPAGE = 0;
            //    c.LASTPAGE = pagecount - 1;
            //    return c;
            //}).ToList();
        }


        public int DeleteDownloadFile(string copname, string fid)
        {
            int _fid = 0;
            if (!Int32.TryParse(fid, out _fid))
            {
                return 0;
            }
            ICompanyDownloadAccess service = new CompanyDownloadDataPump();
            return service.DeleteDownloadFile(copname, _fid);
        }


        public bool DownloadAddItem(string id, DownloadDataPack datapack)
        {
            var result = ServerProvider.Tools.UploadFiles(
                   new List<UploadFileDataPack>() { 
                        new UploadFileDataPack(){
                            BasePath = String.Format(Empires.CRL.Tools.Utils.ReadAppSetting("CustomerFile"),id),
                            TypeFilter= new List<FileType>(){ FileType.All },
                            UploadName="URL",
                            CopyMode= CopyMode.rename
                        }
                    }, System.Web.HttpContext.Current.Request.Files)["URL"];
            datapack.URL = result;
            ICompanyDownloadAccess service = new CompanyDownloadDataPump();
            return service.DownloadAddItem(id, datapack);
        }
    }
}
